// pages/profile-bloghistory/profile-bloghistory.js
import formatTime from "../../utils/formatTime.js";
const app = getApp();
const MAX_LIMIT = 10;
const db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    blogList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this._getListByCloudFn();
    this._getListMiniprogram();
  },

  _getListByCloudFn() {
    wx.showLoading({
      title: '加载中',
    });

    wx.cloud.callFunction({
      name: "blog",
      data: {
        $url: "getListByOpenid",
        start: this.data.blogList.length,
        count: MAX_LIMIT
      }
    }).then((res) => {
      wx.hideLoading();
      // console.log(res);
      this.setData({
        blogList: this.data.blogList.concat(res.result)
      });
    });
  },

  _getListMiniprogram() {
    wx.showLoading({
      title: '加载中',
    });
    db.collection("blog")
      .skip(this.data.blogList.length)
      .limit(MAX_LIMIT)
      .orderBy('createTime', 'desc')
      .get()
      .then((res) => {
        wx.hideLoading();
        // console.log(res);
        let list = res.data;
        for (let i = 0, len = list.length; i < len; i++) {
          list[i].createTime = formatTime(new Date(list[i].createTime));
        }
        this.setData({
          blogList: this.data.blogList.concat(list)
        });
      })
  },
  // 界面跳转
  goComment(e) {
    // console.log(e);
    wx.navigateTo({
      url: `../blog-comment/blog-comment?id=${e.target.dataset.blogid}`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    // this._getListByCloudFn();
    this._getListMiniprogram();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(event) {
    let data = event.target.dataset.blog;
    return {
      title: data.content,
      path: `/pages/blog-comment/blog-comment?id=${data._id}`,
      // imageUrl: ``
    }
  }
})